﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.StsAdmin;
using SoftVenture.Sharepoint.Permissions;

namespace SoftVenture.Sharepoint.STSAdm.Commands.Permissions
{
    class File: ISPStsadmCommand
    {
        private string _url;
        private string _docLib;
        private string _fileName;
        private string _operation;
        private bool _resetRoleInheritance = false;
        private string _account;
        private string _role;
        private bool _breakRoleInheritance = false;
        #region ISPStsadmCommand Members

        public string GetHelpMessage(string command)
        {
            ///TODO: Colocar introdução.
            ///
            string retMsg = System.Environment.NewLine + System.Environment.NewLine;
            retMsg += "Allows to manage file permissions in a document list. At the moment it just supports the root folder in the specified document library " + System.Environment.NewLine;
            retMsg += System.Environment.NewLine;
            retMsg += "Parameters:";
            retMsg += "-url <url>" + System.Environment.NewLine;
            retMsg += "\t-docLib <document library name>" + System.Environment.NewLine; 
            retMsg += "\t[-fileName <file name>]" + System.Environment.NewLine;
            retMsg += "\t-operation <inheritance | list | add | remove | removeAll>" + System.Environment.NewLine;
            retMsg += "\t\t-account <user | group>" + System.Environment.NewLine;
            retMsg += "\t\t-role <role name>" + System.Environment.NewLine;
            retMsg += "\t\t[-BreakRoleInheritance <copyRole | noCopyRole>]" + System.Environment.NewLine;
            retMsg += "\t\t[-ResetRoleInheritance]" + System.Environment.NewLine;
            retMsg += System.Environment.NewLine + System.Environment.NewLine + System.Environment.NewLine;
            retMsg += "Copyright 2010 SoftVenture SA" + System.Environment.NewLine;
            retMsg += "  > For more information on this command and others:" + System.Environment.NewLine;
            retMsg += "  > http://www.softventure.pt/" + System.Environment.NewLine;
            retMsg += "  > Use of this command is at your own risk." + System.Environment.NewLine;
            retMsg += "  > Please, send your problems and comments to product_support@softventure.pt" + System.Environment.NewLine;
            retMsg += "  > SoftVenture SA assumes no liability." + System.Environment.NewLine;

            return retMsg;
        }

        public int Run(string command, System.Collections.Specialized.StringDictionary keyValues, out string output)
        {
            output = string.Empty;
            command = command.ToLowerInvariant();

            #region Validate Input

            if (!keyValues.ContainsKey("url"))
            {
                throw new InvalidOperationException("The url parameter was not specified.");
            }

            this._url = keyValues["url"];

            if (!keyValues.ContainsKey("docLib"))
            {
                throw new InvalidOperationException("The docLib parameter was not specified.");
            }

            this._docLib = keyValues["docLib"];

            if (keyValues.ContainsKey("fileName"))
                this._fileName = keyValues["fileName"];

            if (!keyValues.ContainsKey("operation"))
            {
                throw new InvalidOperationException("The account parameter was not specified.");
            }
            this._operation = keyValues["operation"];


            if (keyValues.ContainsKey("operation") && this._operation != "inheritance" && !keyValues.ContainsKey("account"))
            {
                throw new InvalidOperationException("The account parameter was not specified.");
            }

            this._account = keyValues["account"];


            if (keyValues.ContainsKey("operation") && this._operation != "inheritance" && !keyValues.ContainsKey("role"))
            {
                throw new InvalidOperationException("The role parameter was not specified.");
            }

            this._role = keyValues["role"];

            if (keyValues.ContainsKey("operation") && this._operation == "inheritance")
            {
                this._resetRoleInheritance = keyValues.ContainsKey("ResetRoleInheritance");
                this._breakRoleInheritance = keyValues.ContainsKey("BreakRoleInheritance");
            }
            #endregion

            FileHelper fh = null;

            if (string.IsNullOrEmpty(_fileName))
                fh = new FileHelper(_url, _docLib);
            else
                fh = new FileHelper(_url, _docLib, _fileName);

            switch (_operation.ToLowerInvariant())
            {
                case "add":
                    /// Chamar a biblioteca de permissionamento da lista
                    fh.Add(_role, _account, ref output);

                    break;
                case "remove":
                    /// Chamar a biblioteca de permissionamento do item da lista
                    /// 
                    fh.Remove(_role, _account, ref output);
                    break;
                case "removeall":
                    /// Chamar a biblioteca de permissionamento do item da lista
                    /// 
                    fh.RemoveAll(_account, ref output);
                    break;
                case "list":
                    fh.List(false, ref output);
                    break;
                case "inheritance":
                    if (this._resetRoleInheritance)
                        fh.ResetRoleInheritance(ref output);
                    else
                        if (this._breakRoleInheritance)
                        {
                            switch (keyValues["BreakRoleInheritance"].ToLowerInvariant())
                            {
                                case "copyrole":
                                    fh.RoleInheritance = true;
                                    break;
                                case "nocopyrole":
                                    fh.RoleInheritance = false;
                                    break;
                                default:
                                    throw new InvalidOperationException("Invalid BreakRoleInheritance value.");
                            }

                            fh.BreakRoleInheritance(ref output);
                        }

                    break;
                default:
                    throw new InvalidOperationException("Invalid operation value.");
            }
            fh.Dispose();


            return 0;
        }

        #endregion
    }
}
